﻿#include "kbcreatefolderwizard.h"
#include "ui_kbcreatefolderwizard.h"
#include <QMessageBox>
#include <QTreeWidgetItemIterator>
#include <QDebug>
#include <QDir>
#include "globle.h" // qapp

KbCreateFolderWizard::KbCreateFolderWizard(QWidget *parent) :
    QWizard(parent),
    ui(new Ui::KbCreateFolderWizard)
{
    ui->setupUi(this);
}

KbCreateFolderWizard::~KbCreateFolderWizard()
{
    delete ui;
}

int KbCreateFolderWizard::nextId() const
{
	int pageIndex = currentId();

	switch (pageIndex) {
		case 1:
            //QMessageBox::information(0,"Info","1");
            return 3;
		case 3:
            //QMessageBox::information(0,"Info","3");
            return 5;
		case 5:
           // QMessageBox::information(0,"Info","5");
            return 7;
        case 7:
            //QMessageBox::information(0,"Info","7");
            return -1;
		default:
			return -1 ;
	}
}

void KbCreateFolderWizard::accept()
{
    QMessageBox::information(0,"Info","accept");
    QDialog::accept();
}

void KbCreateFolderWizard::initializePage(int id)
{
   switch (id) {
   case 1:
       break;
   case 3:
       break;
   case 5:
   {
        QTreeWidget *treeWidget = this->ui->treeWidgetDirs;
        treeWidget->setColumnCount(3);
        QList<QTreeWidgetItem *> items;
        //for (int i = 0; i < 10; ++i)
        //    items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString("item: %1").arg(i))));

        // 默认的分类方式
        QStringList headers;
        headers << tr("No.") << tr("Category") << tr("Desc.");
        treeWidget->setHeaderLabels(headers);

        QStringList strListItem;
        strListItem << tr("10") << tr("Job") << tr("Stuffs about job");
        QTreeWidgetItem* level10 =new QTreeWidgetItem((QTreeWidget*)0, strListItem);
        items.append(level10);
        items.append(new QTreeWidgetItem(level10
                                         , QStringList() << tr("11") << tr("Metting") << tr("meetings info")));
        items.append(new QTreeWidgetItem(level10
                                         , QStringList() << tr("12") << tr("Task") << tr("tasks")));
        items.append(new QTreeWidgetItem(level10
                                         , QStringList() << tr("13") << tr("Information") << tr("Informations")));

        treeWidget->insertTopLevelItems(0, items);
   }
       break;
   case 7:
   {
       qDebug() << "start create dirs...";

       QTreeWidget *treeWidget = this->ui->treeWidgetDirs;

       QTreeWidgetItemIterator it(this->ui->treeWidgetDirs);
       while (*it) {
           qDebug() << (*it)->text(0) <<(*it)->text(1) << (*it)->text(2);
           ++it;
       }

       //
       QString rootDir = qApp->getRootDocDir();
       for (int i = 0;i < treeWidget->topLevelItemCount(); i++)
       {
           createDirs(rootDir,treeWidget->topLevelItem(i));
       }
   }
       break;
   default:
       ;
   }
}

void KbCreateFolderWizard::createDirs(QString parent,QTreeWidgetItem *top)
{
    if (top)
    {
        QString dir1 = "";
        dir1 = parent + "/" + top->text(0) + top->text(1);
        QDir d(dir1);
        d.mkpath(dir1);

        for (int i = 0;i < top->childCount();i++)
        {
            createDirs(dir1,top->child(i));
        }
    }
}
